package com.jian.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jian.project.common.ErrorCode;
import com.jian.project.exception.BusinessException;
import com.jian.project.mapper.DbInterfaceMapper;
import com.jian.project.model.dto.dbinterface.DbInterfaceApiRequest;
import com.jian.project.model.entity.DbInterface;
import com.jian.project.model.entity.User;
import com.jian.project.service.DbInterfaceService;
import com.jian.springbootdemosdk.JApiClient;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 18214
 * @description 针对表【db_interface(接口信息)】的数据库操作Service实现
 * @createDate 2023-02-18 13:52:47
 */
@Service
public class DbInterfaceServiceImpl extends ServiceImpl<DbInterfaceMapper, DbInterface>
        implements DbInterfaceService {
    @Resource
    private JApiClient jApiClient;

    @Override
    public void validDbInterface(DbInterface dbInterface, boolean add) {
        if (dbInterface == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = dbInterface.getId();
        String name = dbInterface.getName();
        String description = dbInterface.getDescription();
        String url = dbInterface.getUrl();
        String requestHeader = dbInterface.getRequestHeader();
        String responseHeader = dbInterface.getResponseHeader();
        Integer status = dbInterface.getStatus();
        String method = dbInterface.getMethod();
        Long userId = dbInterface.getUserId();
        Date createTime = dbInterface.getCreateTime();
        Date updateTime = dbInterface.getUpdateTime();
        Integer isDelete = dbInterface.getIsDelete();

        // 创建时，所有参数必须非空
        if (add) {
            if (ObjectUtils.anyNotNull(id, status, createTime, updateTime, isDelete)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        if (StringUtils.isBlank(method)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"method 不能为空");
        }
    }

    @Override
    public Object invokeApi(DbInterfaceApiRequest dbInterfaceApiRequest, User user) {

        // 根据url匹配相对于的sdk
        return jApiClient.invokeApi(dbInterfaceApiRequest);
    }
}




